package com.youi.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.youi.common.SessionKey;
import com.youi.entity.Sysuser;
import com.youi.entity.Userorderexcel;
import com.youi.entity.vo.UserorderexcelVo;
import com.youi.manage.HttpSessionManage;
import com.youi.service.GoodtypeService;
import com.youi.service.UserorderexcelService;
import com.youi.third.ali.oss.OSSUtil;
import com.youi.util.DateUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.ObjectUtil;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * 导出excel异步操作
 * @author 86135
 *
 */
@Service
public class AsyncExcelService {

	@Autowired
	private GoodtypeService goodtypeService;
	@Autowired
	private UserorderexcelService userorderexcelService;
	
	/**
	 * 订单导出excel
	 * @param request
	 * @param userorderexcelList
	 */
	@Transactional
	@Async
	public void tocreatedExcel(HttpServletRequest request, List<UserorderexcelVo> userorderexcelList) {
		// TODO Auto-generated method stub			
		try {
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			String fileName=new Date().getTime()+".xls";//文件名				
			String fileUrl=FileUploadUtil.getUploadUrl(request, "filedownload"+File.separator+fileName);//生成路径
			WritableWorkbook workbook = Workbook.createWorkbook(new File(fileUrl));
			WritableSheet sheet1=workbook.createSheet("Sheet1", 0);
			WritableFont font=new WritableFont(WritableFont.createFont("微软雅黑"),10);
			WritableCellFormat format=new WritableCellFormat();
			format.setAlignment(Alignment.CENTRE);
			format.setVerticalAlignment(VerticalAlignment.CENTRE);
			Label label=new Label(0, 0, "主订单号", format);				
			sheet1.addCell(label);
			label=new Label(1, 0, "订单号",format);
			sheet1.addCell(label);
			label=new Label(2, 0, "下单时间", format);
			sheet1.addCell(label);
			label=new Label(3, 0, "商家名称", format);
			sheet1.addCell(label);
			label=new Label(4, 0, "商家编码", format);
			sheet1.addCell(label);
			label=new Label(5, 0, "状态", format);
			sheet1.addCell(label);
			label=new Label(6, 0, "序号", format);
			sheet1.addCell(label);
			label=new Label(7, 0, "商品名称", format);
			sheet1.addCell(label);
			label=new Label(8, 0, "商品代码", format);
			sheet1.addCell(label);
			label=new Label(9, 0, "分类编码", format);
			sheet1.addCell(label);
			label=new Label(10, 0, "规格", format);
			sheet1.addCell(label);
			label=new Label(11, 0, "商品市场价", format);
			sheet1.addCell(label);
			label=new Label(12, 0, "商品售价", format);
			sheet1.addCell(label);
			label=new Label(13, 0, "支付金额",format);
			sheet1.addCell(label);
			label=new Label(14, 0, "数量",format);
			sheet1.addCell(label);
			label=new Label(15, 0, "顾客姓名",format);
			sheet1.addCell(label);
			label=new Label(16, 0, "顾客手机",format);
			sheet1.addCell(label);
			label=new Label(17, 0, "配送方式",format);
			sheet1.addCell(label);
			label=new Label(18, 0, "快递公司",format);
			sheet1.addCell(label);
			label=new Label(19, 0, "快递单号",format);
			sheet1.addCell(label);
			label=new Label(20, 0, "收货地址",format);
			sheet1.addCell(label);				
			UserorderexcelVo userorderexcel=null;
			for (int i = 0,j=1; i < userorderexcelList.size(); i++,j++) {
				userorderexcel=userorderexcelList.get(i);
				ObjectUtil.ToChangeNullToEmpty(userorderexcel);
				label=new Label(0, j, userorderexcel.getTopOrderNo(), format);
				sheet1.addCell(label);
				label=new Label(1, j, userorderexcel.getOrderNo(), format);
				sheet1.addCell(label);
				label=new Label(2, j, DateUtil.simpdfyMdHms.format(userorderexcel.getGmtOrder()), format);
				sheet1.addCell(label);
				label=new Label(3, j, userorderexcel.getMerchantName(), format);
				sheet1.addCell(label);
				label=new Label(4, j, userorderexcel.getMerchantCode(), format);
				sheet1.addCell(label);
				label=new Label(5, j, ObjectUtil.getOrderState(userorderexcel.getStatus()), format);
				sheet1.addCell(label);
				label=new Label(6, j, String.valueOf(userorderexcel.getOrderId()), format);
				sheet1.addCell(label);
				label=new Label(7, j, userorderexcel.getGoodsName(), format);
				sheet1.addCell(label);
				label=new Label(8, j, userorderexcel.getGoodsNo(), format);
				sheet1.addCell(label);
				label=new Label(9, j, goodtypeService.getGoodsTypeByGoodsId(userorderexcel.getGoodsId(), 0), format);
				sheet1.addCell(label);						
				label=new Label(10, j,userorderexcel.getSpecification(), format);
				sheet1.addCell(label);
				DecimalFormat ecPrice=new DecimalFormat("0.00");
				label=new Label(11, j, ecPrice.format(userorderexcel.getEcPrice()), format);
				sheet1.addCell(label);
				DecimalFormat salePrice=new DecimalFormat("0.00");
				if (userorderexcel.getSalePrice()!=null) {
					label=new Label(12, j, salePrice.format(userorderexcel.getSalePrice()), format);
					sheet1.addCell(label);
				}							
				DecimalFormat payAmount=new DecimalFormat("0.00");
				label=new Label(13, j, payAmount.format(userorderexcel.getPayableAmount()),format);
				sheet1.addCell(label);
				label=new Label(14, j, String.valueOf(userorderexcel.getCount()),format);
				sheet1.addCell(label);
				label=new Label(15, j, userorderexcel.getConsignee(),format);
				sheet1.addCell(label);
				label=new Label(16, j, userorderexcel.getPhone(),format);
				sheet1.addCell(label);
				label=new Label(17, j, userorderexcel.getDeliveryType(),format);
				sheet1.addCell(label);
				label=new Label(18, j, userorderexcel.getExpress(),format);
				sheet1.addCell(label);
				label=new Label(19, j, userorderexcel.getExpressNo(),format);
				sheet1.addCell(label);
				String address=userorderexcel.getProvince()+userorderexcel.getCity()+userorderexcel.getDistrict()+userorderexcel.getAddress();
				label=new Label(20, j, address,format);
				sheet1.addCell(label);
			}
			workbook.write();
			workbook.close();
//			fileUrl="filedownload/"+fileName;
			InputStream inStream=new FileInputStream(fileUrl);
			byte[]	data = ObjectUtil.readInputStream(inStream);
			String dowmupload=OSSUtil.upload(data, ".xls");
			if (dowmupload!=null&&dowmupload.length()>0) {
				userorderexcelService.saveOrUpdate(new Userorderexcel(sysuser.getId(), 0, dowmupload));
			}
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}						
	}
}
